<?php
namespace Common\Model;
use Think\Model;
class AiModel extends Model {
	
	#数据列表
	function _list($where,$order,$pageSize=10) {
 		$mod = $this;
    	import('ORG.Util.Page');
    	$where_pre = " 1 ";
    	if ($this->_hasField('state')) $where_pre.=" AND state=1 ";
    	
    	$count				 = $mod->where($where)->count();
    	$Page 				 = new \Think\Page($count,$pageSize);
    	$Page->parameter	 = array_map('urlencode',get_page_param($map));
    	$nowPage			 = isset($_REQUEST['p'])?$_REQUEST['p']:1;
    	$list 				 = $mod->where($where)->order($order)->page($nowPage.','.$Page->listRows)->select();
    	$show   			 = $Page->show();
		$is_last_page 		 = $nowPage >= $Page->totalPages ? true : false;		

		foreach ($list as &$l) $l = $this->_format($l);

		$ret['page'] 		 = $show;
		$ret['list'] 		 = $list;
		$ret['is_last_page'] = $is_last_page;
		return $ret;		
	}


	#获得一条数据
	function _get($id) {
		$mod =   $this;
		$where = $this->_hasField('state') ? ' state=1 ' : ' 1=1 ';
		if ( CONTROLLER_NAME  =='Ticket' || CONTROLLER_NAME=='TicketOrder' ) $where = ' 1=1 ';

		if (intval($id)>0) {
			$where .= " AND id={$id}"; 
		} elseif (trim($id)!='') {
		 	$where .=" AND {$id}";
		} else {
			return false;
		}

		$vo	   = $this->where($where)->find();
		if ($vo) {
			return $this->_format($vo);
		} else {
			return false;
		}

	}

	#格式化数据
	function _format($vo) {
		return $vo;
	}

	#判断字段是否存在
	protected function _hasField($field){
		$fields = $this->getDbFields();
		return array_search($field,$fields); 
	}

    /**
     * 设置锁定
     * @param $id
     * @return bool
     */
	function _lock($id){
        $mod = $this;
        $res = $mod->where("id = $id")->setField("is_del",1);
        if($res){
            return true;
        }else{
            return false;
        }
    }




    /**
     * @param int $limit_start
     * @param int $limit_count
     * @param string $where
     * @param string $order
     * @return bool
     */
    public function findAll($limit_start=0,$limit_count=10,$where='',$order=' id desc '){
        $list = $this->where($where)->order($order)->limit("$limit_start,$limit_count")->select();
        return $list;
    }

}